﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using DataAccessLayer.DataInterfaces;

namespace DataAccessLayer
{
    public class LoiDAO : ILoi
    {
        private static QLBaoHanh dataContext;
        private IRepository<Loi> repository;
        public LoiDAO()
        {
            dataContext = new QLBaoHanh();
            repository = new RepositoryDAO<Loi>(dataContext);
        }

        public IEnumerable<Loi> GetAllLoi()
        {
            return repository.GetQuery().OrderBy(aobj => aobj.Id);
        }
        public Loi GetLoiById(int Id)
        {
            return repository.Find(obj => obj.Id == Id).Single();
        }
        public Loi GetLastRecord()
        {
            return dataContext.Lois.OrderByDescending(obj => obj.Id).FirstOrDefault();
        }

        public bool UpdateLoi(Loi inLoi)
        {
            if (inLoi == null) return false;
            Loi upLoi = repository.First(sobj => sobj.Id == inLoi.Id);
            if (upLoi == null) return false;
            upLoi = inLoi;
            return repository.SaveChanges() > 0;
        }
        public bool InsertLoi(Loi inLoi)
        {
            if (inLoi == null) return false;
            repository.Add(inLoi);
            return repository.SaveChanges() > 0;
        }

        public bool DelLoi(int Id)
        {
            Loi objDel = repository.First(obj => obj.Id == Id);
            return DelLoi(objDel);
        }


        public bool DelLoi(Loi inLoi)
        {
            if (inLoi == null) return false;
            repository.Delete(inLoi);
            return repository.SaveChanges() > 0;
        }
    }
}
